package com.hfjava.question.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.hfjava.question.constant.Constants;
import com.hfjava.question.dto.R;
import com.hfjava.question.dto.RUtils;
import com.hfjava.question.interfaces.ClassService;
import com.hfjava.question.interfaces.FaqService;
import com.hfjava.question.pojo.Faq;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/faq")
public class FaqController {


    @Reference(version = "1.0.0",interfaceClass= FaqService.class,check = false)
    private FaqService faqService;

    @CrossOrigin(origins = "http://localhost:8088")
    @RequestMapping(value = "/query",method = {RequestMethod.GET},produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public R queryFaq(@RequestParam(value = "keyword",defaultValue = "",required = false) String title){
        List<Faq> faqList = faqService.findFaqByTitleLike(title);
        return RUtils.returnSuccess(faqList);
    }

    @CrossOrigin(origins = "http://localhost:8088")
    @RequestMapping(value = "/details/{qid}",
            method = {RequestMethod.GET},
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    public R queryOne(@PathVariable Integer qid){
        Faq faq = faqService.findFaqById(qid);
        return RUtils.returnSuccess(faq);
    }

    @CrossOrigin(origins = "http://localhost:8088")
    @RequestMapping(value = "/save",
            method = {RequestMethod.POST},
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    public R saveFaq(@RequestBody Faq faq){
        int retVal = faqService.addFaq(faq);
        if(retVal > 0){
            return RUtils.returnSuccess(Constants.OPERATION_SUCCESS,"保存常见问题信息成功!");
        }
        else{
            return RUtils.returnSuccess(Constants.OPERATION_FAILURE,"保存常见问题信息失败!");
        }
    }

}
